<?php
$auth = Auth::getAuth('current_user');
$db = new Database();
$userCompany = new userQueries();
$company = $userCompany->getCompany($auth['company_id']);
$report_id = $_GET['id'];
$form_id = $_GET['form_id'];

$newForm = new Form($db, $form_id);
$reportDoc = new Report($db, $report_id);
?>
<script type="text/javascript" src="/js/functions/report/report.js"></script>
<div class="fl-create-reports-wrapper">
    <?php  include 'layout/workspace_header.php' ?>
    <div class="contentBar_workspace" style="margin-top:34px;">
        <div class="fl-workspace-wrapper">
            <!-- <div class="fl-option-toolbar">
                <div class="fl-toolbar-left-wrapper">
                    <i class="fa fa-bar-chart-o"></i> <span>Dynamic Test Report</span>
                </div>
                <div class="fl-toolbar-right-wrapper">
                    <ul class="fl-breadcrums">
                        <li><a href="/home"><div class="home icon"></div> <i class="fa fa-caret-right"></i></a></li> 
                        <li><a href="/user_view/organizational_chart">Organizational Chart <i class="fa fa-caret-right"></i></a></li> 
                        <li><a href="/user_view/formbuilder">Form Builder <i class="fa fa-caret-right"></i></a></li> 
                        <li><a href="/user_view/workflow">Workflow</a></li>
                    </ul>
                </div>
            </div> -->
        </div>
        <div class="fl-workspace">
            <div class="padding_5">
                <div class="workspace report_ws" style="background: none;position: relative;padding:10px;height: 100%; background-color: #FFF; width:1044px; -webkit-box-sizing:border-box; -moz-box-sizing:border-box; box-sizing:border-box;">
                    <!-- Node -->
                    <div class="fields_below">
                        <div class="label_below">Parameters: </div>
                        <input type="text" class="display" id="active_fields" value="<?php echo $newForm->active_fields; ?>">
                        <input type="button" class="btn-blueBtn fl-add-parameter-btn" action="append_parameter" value="Add Parameter">
                        <div class="input_position_below fl-add-param" style="margin-top: 5px;">
                            <div style="height: auto; margin-bottom:20px; width: 100%;">
                                <table data="data-parameter" class="table_data display_data dataTable">
                                    <thead class="fl-header-tbl-wrapper">
                                    <tr>
                                        
                                            <th>Label</th>
                                            <th>FieldName</th>
                                            <th>Operator</th>
                                            <th>Action</th>
                                        
                                    </tr>
                                    </thead>
                                    <?php
                                    if ($reportDoc->id == 0) {
                                        //new report
                                        ?>
                                        <tr data-type="tr_parameters">
                                            <td><input type="text" column="label" class="form-text"/></td>
                                            <td>
                                                <select column="field" class="form-select">
                                                    <?php
                                                    $fields = explode(",", $newForm->active_fields);
                                                    foreach ($fields as $key => $value) {
                                                        ?>
                                                        <option  value="<?php echo $value; ?>">
                                                            <?php
                                                            echo $value;
                                                            ?>
                                                        </option>
                                                        <?php
                                                    }
                                                    ?>
                                                </select>
                                            </td>
                                            <td>
                                                <select column="operator" class="form-select">
                                                    <option value="=">Equal (==)</option>
                                                    <option value="<=">Less than equal (<=)</option>
                                                    <option value=">=">Greater than equal (>=)</option>
                                                    <option value=">">Greater than(>)</option>
                                                    <option value="<">Less than (<)</option>
                                                    <option value="!=">Not equal(!=)</option>
                                                    <option value="%">Contains(%)</option>
                                                </select>
                                            </td>
                                            <td style="text-align: center;">
                                                <div class="icon-trash fa fa-trash-o cursor deleteParameter" style="margin-left: auto;margin-right: auto" data-type="deactivate" data-user-id="1"></div>
                                            </td>
                                        </tr>   
                                        <?php
                                    } else {
                                        $parameter_arr = json_decode($reportDoc->parameters, true);
                                        foreach ($parameter_arr as $parameter) {
                                            ?>
                                            <tr data-type="tr_parameters">
                                                <td><input type="text" column="label" value="<?php echo $parameter['Column']; ?>" class="form-text"/></td>
                                                <td>
                                                    <select column="field" class="form-select">
                                                        <?php
                                                        $fields = explode(",", $newForm->active_fields);
                                                        foreach ($fields as $key => $value) {
                                                            ?>
                                                            <option  value="<?php echo $value; ?>"
                                                            <?php
                                                            if ($parameter['Field'] == $value) {
                                                                echo 'selected';
                                                            }
                                                            ?>
                                                                     >
                                                                         <?php
                                                                         echo $value;
                                                                         ?>
                                                            </option>
                                                            <?php
                                                        }
                                                        ?>
                                                    </select>
                                                </td>
                                                <td>
                                                    <select column="operator" class="form-select">
                                                        <?php
                                                        if ($parameter['Operator'] == '=') {
                                                            ?>
                                                            <option value="=" selected>Equal (==)</option>
                                                            <?php
                                                        } else {
                                                            ?>
                                                            <option value="=">Equal (==)</option>
                                                            <?php
                                                        }

                                                        if ($parameter['Operator'] == '<=') {
                                                            ?>
                                                            <option value="<=" selected>Less than equal (<=)</option>
                                                            <?php
                                                        } else {
                                                            ?>
                                                            <option value="<=">Less than equal (<=)</option>
                                                            <?php
                                                        }

                                                        if ($parameter['Operator'] == '>=') {
                                                            ?>
                                                            <option value=">=" selected>Greater than equal (>=)</option>
                                                            <?php
                                                        } else {
                                                            ?>
                                                            <option value=">=">Greater than equal (>=)</option>
                                                            <?php
                                                        }

                                                        if ($parameter['Operator'] == '>') {
                                                            ?>
                                                            <option value=">" selected>Greater than(>)</option>
                                                            <?php
                                                        } else {
                                                            ?>
                                                            <option value=">">Greater than(>)</option>
                                                            <?php
                                                        }

                                                        if ($parameter['Operator'] == '<') {
                                                            ?>
                                                            <option value="<" selected>Less than (<)</option>
                                                            <?php
                                                        } else {
                                                            ?>
                                                            <option value="<">Less than (<)</option>
                                                            <?php
                                                        }

                                                        if ($parameter['Operator'] == '!=') {
                                                            ?>
                                                            <option value="!=" selected>Not equal(!=)</option>
                                                            <?php
                                                        } else {
                                                            ?>
                                                            <option value="!=">Not equal(!=)</option>
                                                            <?php
                                                        }

                                                        if ($parameter['Operator'] == '%') {
                                                            ?>
                                                            <option value="%" selected>Contains(%)</option>
                                                            <?php
                                                        } else {
                                                            ?>
                                                            <option value="%">Contains(%)</option>
                                                            <?php
                                                        }
                                                        ?>
                                                    </select>
                                                </td>
                                                <td style="text-align: center;">
                                                    <div class="icon-trash fa fa-trash-o cursor deleteParameter" style="margin-left: auto;margin-right: auto" data-type="deactivate" data-user-id="1"></div>
                                                </td>
                                            </tr>
                                            <?php
                                        }
                                    }
                                    ?>
                                </table>
                            </div>
                        </div>
                    </div>
                    <div class="fields_below">
                        <div class="label_below">Columns: </div>
                        <input type="button" class="btn-blueBtn fl-add-parameter-btn" action="add_column" value="Add Column">      
                        <label style="font-style: italic;color: #ff0000; position:relative; top:18px;">You can sort column headers via drag and drop.</label>
                        <div class="input_position_below" style="margin-top: 5px;float:left">
                            <div style="height: 70px; margin-bottom:20px; width: 100%; border: 1px Solid #ccc;overflow: auto;">
                                <table id="report_columns" class="table_data display_data dataTable" width="400px">
                                    <tr class="sortableRow">
                                    </tr>
                                </table>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                    <div class="fields_below">
                        <div class="label_below">Chart Settings: </div>
                        <input type="button" class="btn-blueBtn fl-add-parameter-btn" action="add_plot_bond" value="Add Plot Band"> 
                        <div style="float: right">
                            <?php
                            if ($reportDoc->id == 0) {
                                $y_axis_max = 10;
                                $y_axis_min = 0;
                                $y_axis_interval = 1;
                            } else {
                                $y_axis_max = $reportDoc->yaxis_max;
                                $y_axis_min = $reportDoc->yaxis_min;
                                $y_axis_interval = $reportDoc->yaxis_interval;
                            }
                            ?>
                            <label>Y-Axis Max:</label><input type="number" value="<?php echo $y_axis_max; ?>" name="yAxisMax" id="yAxisMax"/>
                            <label>Y-Axis Min:</label><input type="number" value="<?php echo $y_axis_min; ?>"  name="yAxisMin" id="yAxisMin"/>
                            <label>Y-Axis Interval:</label><input type="number" value="<?php echo $y_axis_interval; ?>"  name="yAxisInterval" id="yAxisInterval"/>
                        </div>
                        <div class="input_position_below" style="margin-top: 5px">
                            <div style="height: 100%; margin-bottom:20px; width: 100%;">
                                <table id="report_chartPlotBands" class="table_data display_data dataTable" width="400px">
                                    <tr>
                                    <thead class="fl-header-tbl-wrapper">
                                    <th style="border-bottom: 1px solid #D6D6D6  !important;">Name</th>
                                    <th>From Type</th>
                                    <th>From</th>
                                    <th>To Type</th>
                                    <th>To</th>
                                    <th>Color</th>
                                    <th style="border-bottom: 1px solid #D6D6D6 !important;">Action</th>
                                    </thead>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
                <input id="report_id" class="display" type="text" value="<?php echo $report_id ?>" />
                <input id="form_id" class="display" type="text" value="<?php echo $form_id ?>" />
                <input id="parameters" class="display" type="text" value='<?php echo $reportDoc->parameters ?>' />
                <input id="columns" class="display" type="text" value='<?php echo $reportDoc->columns ?>' />
                <div id="column-data" class="display">
                    <?php echo $reportDoc->columns ?>
                </div>
                <div id="plot-bands-data" class="display">
                    <?php echo $reportDoc->plotbands ?>
                </div>
            </div>
            <?php include 'layout/form_controls.php' ?>
            <!-- <div class="fl-form-properties-wrapper">
                <div class="fl-widget-head">
                    <span class="widget-title"><i class="fa fa-cogs"></i> properties</span>
                    <div class="fl-form-properties-toggle">
                        <i class="fa fa-chevron-right"></i>
                    </div>
                </div>
                <div class="fl-props-container">
                    <div class="fl-form-setting-wrapper">
                        <div class="fl-widget-head">
                            <span class="widget-title">Form Settings</span>
                            <span class="fl-min-max-properties" fl-show-opt="true"><i class="fa fa-plus"></i></span>
                        </div>
                        <div class="fl-form-setting-content fl-for-scroll">
                            <label>Custom Form Size</label>
                            <div class="fl-option-form-wrapper">
                                <input type="text" style="float:left;" placeholder="Width" value="1030">
                                <input type="text" style=" float:right;" placeholder="Width" value="600">
                                <div class="clearfix"></div>
                            </div>
                            <div class="fl-option-form-wrapper">
                                <label>Form Size</label>
                                <select class="form-select form_size">
                                    <option value="8.5x11">Letter(8.5in x 11in)</option> 
                                    <option value="8.5x14">Legal(8.5in x 14in)</option> 
                                    <option value="7.25x10.5">Executive(7.25in x 10.5in)</option> 
                                    <option value="8.27x11.69">A4(8.27in x 11.69in)</option> 
                                    <option value="33.11x46.61">A0(33.11in x 46.61in)</option> 
                                    <option value="23.39x33.11">A1(23.39in x 33.11in)</option> 
                                    <option value="16.53x23.39">A2(16.53in x 23.39in)</option> 
                                    <option value="11.69x16.53">A3(11.69in x 16.53in)</option> 
                                    <option value="8.27x11.69">A4(8.27in x 11.69in)</option> 
                                </select>	
                            </div>
                            <div class="fl-option-form-wrapper">
                                <label>Show Ruler</label><Br/>
                                <label>Yes: <input type="radio" value="yes" class="f-show-ruler" name="show-form-ruler-select"></label>
                                <label>No: <input type="radio" value="no" checked="checked" class="f-show-ruler" name="show-form-ruler-select"></label>

                            </div>
                        </div>
                    </div>
                    <div class="fl-form-actions-wrapper">
                        <div class="fl-widget-head">
                            <span class="widget-title">Form Actions</span>
                            <span class="fl-min-max-properties" fl-show-opt="true"><i class="fa fa-plus"></i></span>
                        </div>
                        <div class="fl-form-actions-content fl-for-scroll">
                            <button class="btn-basicBtn padding_5 cursor save_workspace tip fl-command-btn" data-original-title="Save Report" data-workspace="Report" data-form-type="report" style="width: 49%;font-size: 11px;"><i class="icon-save"></i> Save</button>
                            <button class="btn-basicBtn padding_5 cursor clear_all_nodes fl-command-btn tip" data-original-title="Clear Workspace"><i class="fa fa-times"></i> Clear All</button>
                            <button class="btn-basicBtn padding_5 cursor preview_form tip fl-command-btn" data-original-title="Preview Organizational Chart" data-form-type="organizational_chart"><i class="fa fa-search"></i> Preview</button>	
                        </div>
                    </div>
                </div>
            </div> --><!-- /end of fl-form-properties -->
        </div>	
    </div>